package y23.Oct;

public class Test9 {
    public static void main(String[] args) {
        System.out.println(new Solution().StrToInt(" "));
        System.out.println(new Solution().StrToInt(null));
        System.out.println(new Solution().StrToInt(""));
    }
}

class Solution {
    public int StrToInt(String str) {
        if (str == null) {
            return 0;
        }
        try {
            int sum = 0;
            int c = 1;
            if (str.charAt(0) == '-') {
                c = -1;
                sum = num(str, 1);
            } else if (str.charAt(0) == '+') {
                sum = num(str, 1);
            } else {
                sum = num(str, 0);
            }
            return c * sum;
        }catch (StringIndexOutOfBoundsException e){
            e.printStackTrace();
            return 0;
        }
    }

    public int num(String str, int i) {
        int sum = 0;
        for (; i < str.length(); i++) {
            if (str.charAt(i) < '0' || str.charAt(i) > '9') {
                return 0;
            }
            sum = sum * 10 + (str.charAt(i) - '0');
        }
        return sum;
    }
}
